package com.xunlai.infra.permission.business.service.impl;

import com.xunlai.infra.permission.business.converter.MembershipsGoodsConverter;
import com.xunlai.infra.permission.business.dao.entity.MembershipsGoods;
import com.xunlai.infra.permission.business.dao.repository.MembershipsGoodsRepository;
import com.xunlai.infra.permission.business.service.MembershipsGoodsService;
import com.xunlai.infra.permission.business.vo.memberships.MembershipsGoodsCreateVo;
import com.xunlai.infra.permission.business.vo.memberships.MembershipsGoodsListVo;
import com.xunlai.infra.permission.common.vo.BasePageRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author liang
 * @date 8/30/23 9:22 PM
 */
@Service
public class MembershipsGoodsServiceImpl implements MembershipsGoodsService {

    private MembershipsGoodsRepository membershipsGoodsRepository;

    @Override
    public Long createMembershipsGoods(MembershipsGoodsCreateVo membershipsGoodsCreateVo) {
        MembershipsGoods membershipsGoods = MembershipsGoodsConverter.INSTANCE.convertCreateVo2Entity(membershipsGoodsCreateVo);
        MembershipsGoods save = membershipsGoodsRepository.save(membershipsGoods);
        return save.getId();
    }

    @Override
    public void updateMembershipsGoods(Long id, MembershipsGoodsCreateVo membershipsGoodsCreateVo) {
        Optional<MembershipsGoods> membershipsGoodsOptional = membershipsGoodsRepository.findById(id);
        if(membershipsGoodsOptional.isEmpty()){
            return;
        }
        MembershipsGoods membershipsGoods = membershipsGoodsOptional.get();
        BeanUtils.copyProperties(membershipsGoodsCreateVo,membershipsGoods);
        membershipsGoodsRepository.save(membershipsGoods);
    }

    @Override
    public void deleteMembershipsGoods(Long id) {
        membershipsGoodsRepository.deleteById(id);
    }

    @Override
    public BasePageRespVo<MembershipsGoodsListVo> listMembershipsGoods(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        int size = (pageSize == null || pageSize <= 0) ? 10 : pageSize;
        Page<MembershipsGoods> membershipsGoodsPage = membershipsGoodsRepository.findAll(PageRequest.of(pageNum - 1, size));
        List<MembershipsGoodsListVo> membershipsGoodsListVos = membershipsGoodsPage.getContent().stream().map(m -> {
            MembershipsGoodsListVo membershipsGoodsListVo = MembershipsGoodsConverter.INSTANCE.convertEntity2ListVo(m);
            Integer timeLimit = m.getTimeLimit();
            Integer limitUnit = m.getLimitUnit();
            String limit = limitUnit == null ? "" : (limitUnit == 0 ? "年" : (limitUnit == 1 ? "月" : "日"));
            limit = timeLimit + limit;
            membershipsGoodsListVo.setLimit(limit);
            return membershipsGoodsListVo;
        }).toList();
        return new BasePageRespVo<>(membershipsGoodsListVos,pageNum,pageSize,membershipsGoodsPage.getTotalElements(),membershipsGoodsPage.getTotalPages());
    }

    @Autowired
    public void setMembershipsGoodsRepository(MembershipsGoodsRepository membershipsGoodsRepository) {
        this.membershipsGoodsRepository = membershipsGoodsRepository;
    }
}
